<?php

namespace app\api\services\admin\setting;

use app\api\common\service\BaseServices;
use app\api\common\traits\ServicesTrait;
use app\api\common\utils\Util;
use app\api\dao\admin\setting\ConfigDao;
use think\Exception;

/**
 * 配置服务
 *
 * @author Mr.April
 * @since  1.0
 */
class ConfigServices extends BaseServices
{
    use ServicesTrait;

    /**
     * 构造方法
     * PermissionServices constructor.
     *
     * @param \app\api\dao\admin\setting\ConfigDao $dao
     */
    public function __construct(ConfigDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 默认配置数据
     *
     * @param array  $where
     * @param string $field
     *
     * @return array
     */
    public function getByDefault(array $where, string $field = 'value'): array
    {
        $config = json_decode($this->dao->value($where, $field), true);
        if (empty($config)) {
            $config = file_get_contents(app()->getRootPath() . 'public/lms_admin/config/pear.config.json');
            if ($config) {
                $config          = json_decode($config, true);
                $stdClass        = new \stdClass();
                $stdClass->name  = $where['name'];
                $stdClass->value = json_encode($config);
                $this->dao->save((array)$stdClass);
            }
        }
        return $config;
    }

    /**
     * @param $data
     *
     * @return mixed
     * @throws \think\Exception
     */
    public function systemConfig($data): mixed
    {
        $config = $this->getByDefault(['name' => 'system_config']);
        foreach ($data as $key => $items) {
            if (!isset($config[$key])) {
                continue;
            }
            switch ($key) {
                case 'logo':
                    $data[$key]['title'] = htmlspecialchars($items['title'] ?? '');
                    $data[$key]['image'] = $items['image'] ?? '';
                    break;
                case 'menu':
                    $data[$key]['data']         = Util::filterUrlPath($items['data'] ?? '');
                    $data[$key]['accordion']    = !empty($items['accordion']);
                    $data[$key]['collapse']     = !empty($items['collapse']);
                    $data[$key]['control']      = !empty($items['control']);
                    $data[$key]['controlWidth'] = (int)$items['controlWidth'] ?? 500;
                    $data[$key]['select']       = (int)$items['select'] ?? 0;
                    $data[$key]['async']        = true;
                    break;
                case 'tab':
                    $data[$key]['enable']         = true;
                    $data[$key]['keepState']      = !empty($items['keepState']);
                    $data[$key]['preload']        = !empty($items['preload']);
                    $data[$key]['session']        = !empty($items['session']);
                    $data[$key]['max']            = Util::filterNum($items['max'] ?? '30');
                    $data[$key]['index']['id']    = Util::filterNum($items['index']['id'] ?? '0');
                    $data[$key]['index']['href']  = Util::filterUrlPath($items['index']['href'] ?? '');
                    $data[$key]['index']['title'] = htmlspecialchars($items['index']['title'] ?? '首页');
                    break;
                case 'theme':
                    $data[$key]['defaultColor']  = Util::filterNum($items['defaultColor'] ?? '2');
                    $data[$key]['defaultMenu']   = $items['defaultMenu'] ?? '' == 'dark-theme' ? 'dark-theme' : 'light-theme';
                    $data[$key]['defaultHeader'] = $items['defaultHeader'] ?? '' == 'dark-theme' ? 'dark-theme' : 'light-theme';
                    $data[$key]['allowCustom']   = !empty($items['allowCustom']);
                    $data[$key]['banner']        = !empty($items['banner']);
                    break;
                case 'colors':
                    foreach ($config['colors'] as $index => $item) {
                        if (!isset($items[$index])) {
                            $config['colors'][$index] = $item;
                            continue;
                        }
                        $data_item                    = $items[$index];
                        $data[$key][$index]['id']     = $index + 1;
                        $data[$key][$index]['color']  = $this->filterColor($data_item['color'] ?? '');
                        $data[$key][$index]['second'] = $this->filterColor($data_item['second'] ?? '');
                    }
                    break;

            }
        }
        $config = array_merge($config, $data);
        $name   = 'system_config';
        $value  = ['value' => json_encode($config)];
        return $this->dao->update($name, $value, 'name');
    }

    /**
     * 颜色检查
     *
     * @param string $color
     *
     * @return string
     * @throws \think\Exception
     */
    protected function filterColor(string $color): string
    {
        if (!preg_match('/\#[a-zA-Z]6/', $color)) {
            throw new Exception('参数错误');
        }
        return $color;
    }
}
